﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneMake.Model;
using OneMake.DataAccess;

namespace OneMake.Model
{
    //全网各类目销售曲线图
    public class report_qw_type_sale_chart
    {
        public List<string> titles { get; set; }
        public Dictionary<DateTime, Dictionary<string, int>> values { get; set; }
        public report_qw_type_sale_chart()
        {
            titles = new List<string>();
            values = new Dictionary<DateTime, Dictionary<string, int>>();
        }
    }

    public class report_qw_type_sale_chart2
    {
        public int shopid { get; set; }
        public DateTime stat_datetime { get; set; }
        public string category { get; set; }
        public int sel_count { get; set; }
    }

    partial class ReportHelper
    {
        public List<string> get_qw_type_sale_chart_titles(DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select stat_datetime, category from mf_m_qwsczdhq where stat_datetime between @0 and @1 group by category";
            IDataReader reader = db.ExecuteReader(sql, beginTime, endTime);

            List<string> titles = new List<string>();
            titles.Add("销售量");
            while (reader.Read())
            {
                string title = reader["category"].ToString();
                titles.Add(title);
            }
            return titles;
        }

        public report_qw_type_sale_chart get_report_qw_type_sale_chart(DateTime beginTime, DateTime endTime, int shopid)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = @"select shopid,stat_datetime,category,SUM(n_products) as sel_count from mf_m_qwsczdhq 
                        where stat_datetime BETWEEN @0 AND @1 AND shopid=@2
                        group by stat_datetime,category ";
            IEnumerable<report_qw_type_sale_chart2> items = db.Query<report_qw_type_sale_chart2>(sql, beginTime, endTime, shopid);

            report_qw_type_sale_chart report = new report_qw_type_sale_chart();
            report.titles.Add("销售量");

            foreach (report_qw_type_sale_chart2 item in items)
            {
                report.titles.Add(item.category);

                if (!report.values.ContainsKey(item.stat_datetime))
                    report.values.Add(item.stat_datetime, new Dictionary<string, int>());

                Dictionary<string, int> selValues = report.values[item.stat_datetime];
                if (selValues.ContainsKey(item.category))
                    selValues[item.category] += item.sel_count;
                else
                    selValues.Add(item.category, item.sel_count);
            }

            return report;
        }
    }
}

